//本地测试环境
//var url ="http://127.0.0.1:8080/Terminal/rest/Terminal/query";
//测试环境
var url ="http://flymt.ceair.com:80/Terminal/rest/Terminal/query";
//var url ="http://mgstest.ceair.com:8080/Terminal/rest/Terminal/query";
var iG = {};
iG.versionNo = '202022';
iG.debug = false;
/* 操作数据
---------------------------------------------------------------------------------------*/
//AJAX请求
iG.ajax = function(settings){
    var base64Encode = new Base64();
    var url = "http://mobile-am.ceair.com:8080/AirMobileControlRS2/JsonService/";
    if(this.debug) url = "http://mobile-amdev.ceair.com:8080/AirMobileControlRS2/JsonService/";
    var data = JSON.stringify(settings.data);
    var userInfo = iG.getUserInfo();
    var dataJson = {
        jsonStr : base64Encode.encode(data),
        jsonUserInfo : userInfo
    }
    $.ajax({
        type : "post",
        contentType: 'application/json',
        dataType:"json",  
        url : url + "GetJsonDataPost",
        data: JSON.stringify(dataJson),  
        success : function(data){
            settings.success(JSON.parse(data));
        },
        error : function(data){	
            settings.error(data);
        }
    });
};
//AJAX请求
iG.getAjax = function(settings){
    //var base64Encode = new Base64();
    var url = "http://172.31.3.107:8088/mobileSecureR2/rest/querySecInfo";
    if(this.debug) url = "http://mobile-amdev.ceair.com:8088/mobileSecureR2/rest/querySecInfo";
    var dataJson = {
        jsonStr : settings.data,
        jsonUserInfo : iG.getUserInfoStr()
    }
	var dataStr = JSON.stringify(dataJson);
	$.ajax({
        type : "post",
        contentType: 'application/json',
        dataType:"json",
        url : url,
        data: encode64(utf16to8(dataStr)),  
        success : function(data){
            settings.success(data);
        },
        error : function(data){
            settings.error(data);
        }
    });
};
//自动装载数据
iG.autoData = function(obj,wrapElem){
    if(wrapElem == undefined) wrapElem = $("body");
    wrapElem.find("[auto]").each(function(){
        var key = $(this).attr("auto");
        var objCopy = obj;
        while (key.indexOf("_") != "-1") {
            var sep = key.indexOf("_");
            objCopy = objCopy[key.substring(0, sep)];
            key = key.substring(key.indexOf("_") + 1);
        };
        if (objCopy != null) {
            $(this).text(objCopy[key]);
        }
    });
};
/* 操作本地存储
---------------------------------------------------------------------------------------*/
//获取本地存储obj值
//获取本地存储obj值
iG.getObjItem = function(obj,key){
    var json = localStorage.getItem(obj);
    if(json == null || json == undefined || json == "undefined" || json == ""){
		return undefined;
	}else{
		json=JSON.parse(json);
	}
    return json[key];
};
//设置本地存储obj值
iG.setObjItem = function(obj,key,value){
    var json = localStorage.getItem(obj);
    if(json == null || json == undefined || json == "undefined" || json == ""){
		json = {};
	}else{
		json=JSON.parse(json);
	}
    json[key] = value;
    localStorage.setItem(obj,JSON.stringify(json));
};
//删除本地存储obj值
iG.removeObjItem = function(obj,key){
    var json = localStorage.getItem(obj);
    if(json == null || json == undefined || json == "undefined" || json == ""){
		return;
	}else{
		json=JSON.parse(json); 
	}
    delete json[key];
    localStorage.setItem(obj,JSON.stringify(json));
};

/* 操作日期
---------------------------------------------------------------------------------------*/
//获取年
iG.getYear = function(date){
    if(date == undefined) date = new Date;
    return date.getFullYear();
};
//获取月
iG.getMonth = function(date){
    if(date == undefined) date = new Date;
    var month = date.getMonth() + 1;
    if(month < 10) month = "0" + month;
    return month;
};
//获取日
iG.getDay = function(date){
   if(date == undefined) date = new Date;
    var day = date.getDate();
    if(day < 10) day = "0" + day;
    return day;
};
//获取当前日期
iG.getDateToday = function(date){
    return iG.getYear() + "-" + iG.getMonth() + "-" + iG.getDay();
};
//获取星期
iG.getWeek = function(date){
    if(date == undefined) date = new Date;
    var week = date.getDay();
    var weekArr = ["日","一","二","三","四","五","六"];
    return weekArr[week];
};
//获取小时
iG.getHours = function(date){
    if(date == undefined) date = new Date;
    return date.getHours();
},
//获取分钟
iG.getMinutes = function(date){
    if(date == undefined) date = new Date;
    return date.getMinutes();
};
//打开日期控件
iG.openDate = function(year,month,day,callback){
    uexControl.openDatePicker(year,month,day);
    uexControl.cbOpenDatePicker=function(opCode,dataType,data){
        if(dataType==1){
            data = JSON.parse(data);
            if(data.month<10) data.month = "0" + data.month;
            if(data.day<10) data.day = "0" + data.day;
            var json = {
                year : data.year,
                month : data.month,
                day : data.day,
            };
            callback(json);
        }
    }   
};
//打开时间控件
iG.openTime = function(hours,minutes,callback){
    uexControl.openTimePicker(hours,minutes);
    uexControl.cbOpenTimePicker = function(opCode,dataType,data){  
        data = JSON.parse(data);
        if(data.hour < 10) data.hour = "0" + data.hour;
        if(data.minute < 10) data.minute = "0" + data.minute;
        var json = {
            hour : data.hour,
            minute : data.minute
        };
        callback(json);
    }   
};
/* 设置页面回弹
---------------------------------------------------------------------------------------*/
//默认状态
iG.setBounce = function(){
    uexWindow.setBounce("1");
    uexWindow.showBounceView("0","#f4f4f4","0");
    uexWindow.showBounceView("1","#f4f4f4","0");
    uexWindow.notifyBounceEvent("0","0");
};
//下拉刷新
iG.pullDown = function(callback){
    var j = {
        "pullToReloadText":"下拉刷新",
        "releaseToReloadText":"释放立即刷新",
        "loadingText":"正在刷新...",
        "loadingImagePath":"res://shuaxin.png"
    };
    if(!uexWindow.setBounce)
        return;
    uexWindow.setBounce("1");
    uexWindow.setBounceParams('0',JSON.stringify(j));
    uexWindow.showBounceView("0","#f4f4f4",1);
    uexWindow.showBounceView("1","#f4f4f4",0);
    uexWindow.notifyBounceEvent("0","1");
    uexWindow.onBounceStateChange = function(type,status){
        if(type==0 && status==2) {
            callback();
        }
    }
};
/* 其他
---------------------------------------------------------------------------------------*/
//记录打开窗口
iG.saveOpenWin = function(winName){
    var array = iG.getObjItem("openWin","array");
    if(array.indexOf(winName) == -1){
        array.push(winName);
    }
    iG.setObjItem("openWin","array",array);
};
//打开新窗口
iG.openWin = function(name,data){
   /* if(!iG.checkAuthortization("window",name)){
        uexWindow.alert("提示","很抱歉，您没有相关权限！","确定");
        return false;
    }*/
    if(data){
        localStorage.setItem(name,JSON.stringify(data));
    }
    var url = name + ".html";
    iG.saveOpenWin(name);
    uexWindow.open(name,0,url,2,"","","4",280);
};
//获取用户账户
iG.getUserInfo = function(){
    var base64Encode = new Base64();	
    var user = {
        "u": iG.getObjItem("LoginInforAMC","userCode"),
        "p": iG.getObjItem("LoginInforAMC","password"),
        "m": iG.getObjItem("LoginInforAMC","mobile"),
		"t": iG.getObjItem("LoginInforAMC","myTimezone"),
        "a": "MOP"
    };
    var userStr = JSON.stringify(user)
    return base64Encode.encode(userStr);
};
//获取用户账户
iG.getUserInfoStr = function(){	
    var user = {
//        "u": iG.getObjItem("LoginInforAMC","userCode"),
//        "p": iG.getObjItem("LoginInforAMC","password"),
		"u": "chengcheng1",
        "p": "sylar900519",
        "m": iG.getObjItem("LoginInforAMC","mobile"),
		"t": iG.getObjItem("LoginInforAMC","myTimezone"),
        "a": "MOP"
    };
    return user;
};
//页面提示
iG.tip = function(msg, tipId){
    var html = '<div class="uba p10 uc-a5 c17 tx-c">'+ msg +'</div>';
    if (tipId) {
        $("#" + tipId).empty().addClass("p8").append(html);
    }
    else {
        $("body").empty().addClass("p8").append(html);
    }
};
//数据是否合法
iG.isGood = function(data,tipOpen){
    if (data.status == 1) {
        $.iBoxMsg(data.desc);
        return false;
    };
    if (data.array == "" || data.array == null) {
        tipOpen && iG.tip("很抱歉，未查询到相应的" + $("title").text());
        return false;
    };
    return true;
};
//选择机场
iG.airportOption = function(winName,fnName,elemId){
    var data = {
        winName : winName,
        fnName : fnName,
        elemId : elemId
    };
    iG.openWin("terminalSelect_win",data);
}; 
//请求回调
iG.ajaxCallback = function(isRefresh){
    if(isRefresh){
        uexWindow.resetBounceView('0');
    }else{
        $.iBoxHide();
    }
};
//状态颜色
iG.statusColor = {
    "返航" : "c10",//绿色
    "备降" : "c10",//绿色
    "取消" : "c4",//灰色
    "到达" : "c2",//黑色
    "未起飞" : "c18",//蓝色
    "延误" : "c16",//红色
    "起飞" : "c22"//蓝色
};
//是否显示
iG.isShow = function(type){
    $("[showAt]").each(function(){
        var array = $(this).attr("showAt").split(",");
        for(var i in array){
            if(type == array[i]){
                $(this).removeClass("uhide");
                return;
            }
        }
        $(this).addClass("uhide");
    })
    $("[hideAt]").each(function(){
        var array = $(this).attr("hideAt").split(",");
        for(var i in array){
            if(type == array[i]){
                $(this).addClass("uhide");
                return;
            }
        }
        $(this).removeClass("uhide")
    })
};
//固定字符串
iG.fixed = function(){
    $("[fixed]").each(function(){
        var txt = $(this).text();
        $(this).text(txt.substring(0,2))
    })
};
//滚动
iG.scroll = function(wrap,speedVal){
    var speed = speedVal || 50;
    var that = arguments.callee;
    var width = wrap.width();
    var child = wrap.children();
    var childWidth = child.width();
    var time = parseInt(width + childWidth) * 1000 / speed;
//    alert(width)
//    alert(childWidth)
    if(childWidth<= width)return;
    wrap.css("box-pack","start");
    child.css("left",width).animate({"left":-childWidth},time,"linear",function(){
        that(wrap,speed);
    })
};
//检查每月最后一天是几号
iG.checkLastDay = function(year,month){
    var maxDays = 31;
    if(month == 2){
        if(parseInt(year)%4 == 0)maxDays = 29;
        else maxDays = 28;
    }
    if(month == 4 || month == 6 || month == 9 || month == 11)
        maxDays = 30;
    return month + "-" + maxDays;
};
//权限检查
//iG.checkAuthortization = function(type,htmlName){
//    var authorArr = iG.getObjItem("basicData","authorArr");
//    if($.inArray(htmlName,authorArr) == -1){
//        //alert(htmlName + " can not open");
//        return false;
//    }
//    return true;
//};
//debug
if(window.navigator.platform=="Win32" && iG.debug){
    document.write('<script src="../js/debug.js?v=202013"></script>')
};